{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f6b0a176",
   "metadata": {},
   "source": [
    "## 1.1.1 位置与分散程度的度量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ee1898f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as st\n",
    "import pandas as pd\n",
    "import statsmodels.api as sm\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "09866953",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "62.36\n",
      "62.36\n",
      "学生体重的[10%, 20%, 40%, 60%, 80%, 100%]分位数： [52.76 56.98 62.2  64.   67.32 75.  ]\n",
      "体重数据方差的估计为：52.71，无偏估计为：56.47\n",
      "体重数据标准差的估计为：7.26，无偏估计为：7.51\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'w_mean' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 35\u001b[0m\n\u001b[0;32m     32\u001b[0m s_unb \u001b[38;5;241m=\u001b[39m st\u001b[38;5;241m.\u001b[39mtstd(weights) \u001b[38;5;66;03m# 无偏估计\u001b[39;00m\n\u001b[0;32m     33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m体重数据标准差的估计为：\u001b[39m\u001b[38;5;132;01m%0.2f\u001b[39;00m\u001b[38;5;124m，无偏估计为：\u001b[39m\u001b[38;5;132;01m%0.2f\u001b[39;00m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m%\u001b[39m (s, s_unb))\n\u001b[1;32m---> 35\u001b[0m cv \u001b[38;5;241m=\u001b[39m s_unb \u001b[38;5;241m/\u001b[39m \u001b[43mw_mean\u001b[49m \u001b[38;5;241m*\u001b[39m \u001b[38;5;241m100\u001b[39m  \u001b[38;5;66;03m# 变异系数，无量纲，用百分数表示\u001b[39;00m\n\u001b[0;32m     36\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m体重数据的变异系数为：\u001b[39m\u001b[38;5;124m'\u001b[39m, np\u001b[38;5;241m.\u001b[39mround(cv, \u001b[38;5;241m2\u001b[39m), \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m%\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m     38\u001b[0m \u001b[38;5;66;03m# 极差与标准误\u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'w_mean' is not defined"
     ]
    }
   ],
   "source": [
    "# 示例：某学校15个学生体重（单位：公斤）抽样调查数据\n",
    "weights = np.array([75.0, 64.0, 47.4, 66.9, 62.2, 62.2, 58.7,\n",
    "                   63.5, 66.6, 64.0, 57.0, 69.0, 56.9, 50.0, 72.0])\n",
    "\n",
    "# 均值\n",
    "w_mean = np.mean(weights)\n",
    "w_mean2 = weights.mean()\n",
    "print(w_mean)\n",
    "print(w_mean2)\n",
    "\n",
    "# 限定范围内的数据求均值（截断 60 - 70）\n",
    "limitedMean = st.tmean(weights, (60, 70))\n",
    "\n",
    "sorted_weig = sorted(weights, reverse=True)  # reverse 的缺省值为False\n",
    "\n",
    "# 中位数，重要的统计值\n",
    "# 对称分布，比如T分布和正态分布，均值和中位数很接近，偏态分布的二者相差比较大，比如F分布\n",
    "median_weig = np.median(weights)\n",
    "\n",
    "# 分位数\n",
    "quantiles = np.quantile(weights, [0.1, 0.2, 0.4, 0.6, 0.8, 1])\n",
    "print('学生体重的[10%, 20%, 40%, 60%, 80%, 100%]分位数：', quantiles)\n",
    "\n",
    "# 方差、标准差、极差、标准误\n",
    "# 注意：方差与方差的无偏估计之间的计算区别\n",
    "v = np.var(weights) # 有偏估计或样本方差\n",
    "v_unb = st.tvar(weights) # 无偏估计\n",
    "print('体重数据方差的估计为：%0.2f，无偏估计为：%0.2f' % (v, v_unb))\n",
    "\n",
    "# 注意标准差与标准差的无偏估计之间的计算区别\n",
    "s = np.std(weights)  # 有偏估计或样本标准差\n",
    "s_unb = st.tstd(weights) # 无偏估计\n",
    "print('体重数据标准差的估计为：%0.2f，无偏估计为：%0.2f' % (s, s_unb))\n",
    "\n",
    "cv = s_unb / w_mean * 100  # 变异系数，无量纲，用百分数表示\n",
    "print('体重数据的变异系数为：', np.round(cv, 2), '%')\n",
    "\n",
    "# 极差与标准误\n",
    "R_weights = np.max(weights) - np.min(weights)  # 极差 = 最大值 - 最小值\n",
    "print('体重数据的极差：%0.2f' % R_weights)\n",
    "\n",
    "sm_weights = st.tstd(weights) / np.sqrt(len(weights))  # 标准误：数据标准差（无偏）/ 数据量 ** 0.5\n",
    "print('体重数据的标准误：%0.2f' % sm_weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a30fdea4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
